package com.cjzuji.rental.dao.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cjzuji.rental.controller.order.YouPinController;
import com.cjzuji.rental.dao.OrderUserDao;
import com.cjzuji.rental.mapper.OrderUserMapper;
import com.cjzuji.rental.pojo.OrderUser;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author bag
 * @version V1.0
 * @date 2025/7/7 21:20
 * @since 1.0
 */
@Service
public class OrderUserDaoImpl implements OrderUserDao{
    private static final Logger logger = LoggerFactory.getLogger(OrderUserDaoImpl.class);
    @Autowired
    OrderUserMapper orderUserMapper;

    @Override
    @Transactional
    public  void save(Map<String,Object> detail, Long orderID, List<String> userInfo){
        String idcardImgUrl = "";
        String idcardBackImgUrl = "";
        String faceImgUrl = "";
        if (detail.get("idcardImgUrl") != null){
            idcardImgUrl = detail.get("idcardImgUrl").toString();
        }
        if (detail.get("idcardBackImgUrl") != null){
            idcardBackImgUrl = detail.get("idcardBackImgUrl").toString();
        }
        if (detail.get("faceImgUrl") != null){
            faceImgUrl = detail.get("faceImgUrl").toString();
        }
        OrderUser orderUser = OrderUser.builder()
                .orderId(orderID)
                .userName(userInfo.get(0))  //收货人地址
                .mobile(userInfo.get(1))
                .province(detail.get("receiverProvince").toString())
                .city(detail.get("receiverCity").toString())
                .county(detail.get("receiverCounty").toString())
                .address(detail.get("receiverAddress").toString())
                .addressInfo(userInfo.get(2))
                .idCard(userInfo.get(4))
                .idCardName(userInfo.get(0))
                .idCardImg(idcardImgUrl)
                .idCardBlackImg(idcardBackImgUrl)
                .faceImg(faceImgUrl)
                .createTime(new Date())
                .updateTime(new Date())
                .build();
        //logger.info("打印添加参数1 {}",orderUser);
//        orderUserMapper.insert(orderUser);
        try {
            orderUserMapper.insert(orderUser);
        }catch (Exception e){
            logger.info("订单用户添加错误" + e.getMessage());
            throw e; // 重新抛出，确保事务回滚
        }
    }
}
